package phoenix.web.page_objects;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.Select;
import org.testng.TestException;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseWebExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.web.locators.base.IndexPageLocatorsBase;

/**
 * Class provides access to web elements and actions on IndexPage
 */
public class IndexPage extends PageBaseWebExt
{
	/**
	 * Initializes the IndexPage
	 * @param p_manager PhoenixManager instance
	 */
	public IndexPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	/**
	 * Gets web elements by site
	 * @return IndexPageLocatorsBase instance
	 */
	public IndexPageLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_CLICKANDFLIRT:
			{
				return new phoenix.web.locators.clickandflirt.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_LOCALSGOWILD:
			{
				return new phoenix.web.locators.localsgowild.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_EBONYFLIRT:
			{
				return new phoenix.web.locators.ebonyflirt.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.web.locators.cheekylovers.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.web.locators.wildbuddies.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.web.locators.flirt.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_FOREIGNGIRLFRIEND:
			{
				return new phoenix.web.locators.foreigngirlfriend.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_BENAUGHTY:
			{
				return new phoenix.web.locators.benaughty.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_NAUGHTYFEVER:
			{
				return new phoenix.web.locators.naughtyfever.IndexPageLocators();
			}
			case ConfigExt.SITE_ID_MATUREDATING:
			{
				return new phoenix.web.locators.maturedating.IndexPageLocators();
			}
			default:
			{
				return null;
			}
		}
	}

	public void waitForPageLoad()
	{
		if (manager.isNF())
		{
			this.closeIndexPopup();
		}

		wd().waitForVisibility(elements().btnRegSubmit, "Submit Reg button");
	}

	public void closeIndexPopup()
	{
		log().info("Close index popup");

		wd().waitForPresence((elements().btnClosePopup), "Button 'ClosePopup'");
		wd().click(elements().btnClosePopup);
	}

	public void setGender(String p_gender)
	{
		log().info("Set select gender");

		if (manager.isBN() || manager.isFlirt() || manager.isFGF() || manager.isNF())
		{
			By locator = p_gender.equals("male") ? elements().btnGenderMale : elements().btnGenderFemale;
			wd().click(locator);
		}

		else if (manager.isWB() || manager.isMD())
		{
			String text = (p_gender.equals("male")) ? "a man" : "a woman";
			new Select(wd().getWebElement(elements().selectRegGender)).selectByVisibleText(text);
		}
	}

	public void setDay(String p_day)
	{
		log().info("Set select day");

		wd().waitForPresence(elements().selectDay, "Select 'Day'");
		wd().selectByValue(elements().selectDay, p_day);
	}

	public void setMonth(String p_month)
	{
		log().info("Set select month");

		wd().waitForPresence(elements().selectMonth, "Select 'Month'");
		wd().selectByValue(elements().selectMonth, p_month);
	}

	public void setYear(String p_year)
	{
		log().info("Set select year");

		wd().waitForPresence(elements().selectYear, "Select 'Year'");
		wd().selectByValue(elements().selectYear, p_year);
	}

	public void setDateOfBirth(String p_day, String p_month, String p_year)
	{
		this.setDay(p_day);
		this.setMonth(p_month);
		this.setYear(p_year);
	}

	public void setEmail(String p_email)
	{
		log().info("Set field email => " + p_email);
		wd().waitForPresence(elements().txtRegEmail, "Field 'Email'");
		wd().setElementText(elements().txtRegEmail, true, p_email);
	}

	public void setPassword(String p_password)
	{
		log().info("Set field password => " + p_password);
		wd().waitForPresence(elements().txtRegPassword, "Field 'Password'");
		wd().setElementText(elements().txtRegPassword, true, p_password);
	}

	public void setLocation(String p_location)
	{
		log().info("Set field location");

		wd().waitForPresence(elements().txtRegLocation, "Field 'Location'");
		wd().setElementText(elements().txtRegLocation, true, p_location);
	}

	public void clickSubmit()
	{
		log().info("Click button submit");
		wd().waitForPresence(elements().btnRegSubmit, "Button 'Submit'");
		wd().wait(5);
		wd().click(elements().btnRegSubmit);
		wd().wait(5);
	}

	public void submitRegistrationForm()
	{
		this.clickSubmit();
	}

	public void setLoginFormEmail(String p_email)
	{
		log().info("Set login field email");

		wd().waitForPresence(elements().txtLoginEmail, "Field login form 'Email'");
		wd().click(elements().elLoginEmail);
		wd().type(elements().txtLoginEmail, p_email);
	}

	public void setLoginFormPassword(String p_password)
	{
		log().info("Set login field password");

		wd().waitForPresence(elements().txtLoginPassword, "Field login form 'Password'");
		wd().click(elements().elLoginPassword);
		wd().type(elements().txtLoginPassword, p_password);
	}

	public void openLoginForm()
	{
		log().info("Open login form");

		wd().waitForPresence(elements().linkOpenLoginForm, "Button login form 'Open'");
		wd().click(elements().linkOpenLoginForm);
	}

	public void submitLoginForm()
	{
		log().info("Submit login form");
		wd().waitForPresence(elements().btnSubmitLogin, "Button login form 'Submit'");
		wd().wait(1);
		wd().click(elements().btnSubmitLogin);
		wd().wait(2);
	}

	public void fillLoginForm(String p_email, String p_password)
	{
		this.setLoginFormEmail(p_email);
		this.setLoginFormPassword(p_password);
	}

	public void fillMainForm(String p_gender, String p_email, String p_password, String p_day, String p_month, String p_year)
	{
		this.setGender(p_gender);
		this.setDay(p_day);
		this.setMonth(p_month);
		this.setYear(p_year);
		this.setEmail(p_email);
		this.setPassword(p_password);
	}

	public void clickNextStep()
	{
		log().info("Click button next step");
		wd().wait(3);
		wd().click(elements().btnNext);
		wd().wait(3);
	}

	public void fillThreeStepsForm(String p_gender, String p_email, String p_password, String p_day, String p_month, String p_year)
	{
		this.setGender(p_gender);
		this.clickNextStep();
		this.setDay(p_day);
		this.setMonth(p_month);
		this.setYear(p_year);
		this.clickNextStep();
		this.setEmail(p_email);
		this.setPassword(p_password);
	}

	public String getPostcode()
	{
		log().info("Get field location data");

		wd().waitForPresence(elements().txtRegLocation, "Field 'Location'");
		String p_postcode = wd().getAttribute(elements().txtRegLocation, "value");

		if (p_postcode == null)
		{
			throw new TestException("There is no postcode suggestion");
		}

		return p_postcode;
	}

	public boolean isDateValid(boolean p_typeValidation)
	{
		log().info("Get date of birth validation");

		wd().waitForPresence(elements().blockRegForm, "Block 'RegisterForm'");

		if (p_typeValidation)
		{
			if (wd().isElementPresent(elements().blockBirthValidIcon))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if (wd().isElementPresent(elements().blockBirthInvalidIcon))
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}

	public boolean isEmailValid(boolean p_typeValidation)
	{
		log().info("Get email validation");

		wd().waitForPresence(elements().blockRegForm, "Block 'RegisterForm'");

		if (p_typeValidation)
		{
			if (wd().isElementPresent(elements().blockEmailValidIcon))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if (wd().isElementPresent(elements().blockEmailInvalidIcon))
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}

	public boolean isPasswordValid(boolean p_typeValidation)
	{
		log().info("Get password validation");

		wd().waitForPresence(elements().blockRegForm, "Block 'RegisterForm'");

		if (p_typeValidation)
		{
			if (wd().isElementPresent(elements().blockPasswordValidIcon))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if (wd().isElementPresent(elements().blockPasswordInvalidIcon))
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}

	public boolean isLocationValid(boolean p_typeValidation)
	{
		log().info("Get location validation");

		wd().waitForPresence(elements().blockRegForm, "Block 'RegisterForm'");

		if (p_typeValidation)
		{
			if (wd().isElementPresent(elements().blockLocationValidIcon))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if (wd().isElementPresent(elements().blockLocationInvalidIcon))
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}

	public String getLinkTerms()
	{
		log().info("Get link terms");

		wd().waitForPresence(elements().linkTerms, "Link 'Terms'");
		return wd().getAttribute(elements().linkTerms, "href");
	}

	public String getLinkPrivacy()
	{
		log().info("Get link privacy");

		wd().waitForPresence(elements().linkPrivacy, "Link 'Privacy'");
		return wd().getAttribute(elements().linkPrivacy, "href");
	}

	public String getResendEmailText(String p_email)
	{
		log().info("Get text resend email");

		wd().waitForPresence(elements().linkResendEmail, "Link 'ResendEmail'");
		wd().click(elements().linkResendEmail);
		wd().wait(3);

		return wd().getText(elements().blockEmailErrorMessage);
	}

	public void clickLoginFormForgotPassword()
	{
		log().info("Click login form link forgot password");
		wd().waitForPresence(elements().linkForgotPassword, "Link login form 'ForgotPassword'");
		wd().click(elements().linkForgotPassword);
	}

	public void clickLoginFormSend()
	{
		log().info("Click login form button Send");

		wd().waitForPresence(elements().btnLoginSend, "Button login form 'Send'");
		wd().click(elements().btnLoginSend);
	}

	public void typeLoginEmail(String p_value)
	{
		wd().waitForVisibility(elements().txtEmailB2b, "Field login form B2B 'Email'");
		wd().type(elements().txtEmailB2b, p_value);
	}

	public void typeLoginPassword(String p_value)
	{
		wd().waitForVisibility(elements().txtPasswordB2b, "Field login form B2B 'Password'");
		wd().type(elements().txtPasswordB2b, p_value);
	}

	public void submitLoginFormB2B()
	{
		wd().waitForVisibility(elements().btnLoginSubmitB2b, "Button login form B2B 'Submit'");
		wd().click(elements().btnLoginSubmitB2b);
	}
}